package nancal.mp

import com.zaxxer.hikari.HikariDataSource
import integration.TestBase
import io.kubernetes.client.openapi.Configuration
import io.kubernetes.client.openapi.apis.CustomObjectsApi
import io.kubernetes.client.util.ClientBuilder
import io.kubernetes.client.util.credentials.AccessTokenAuthentication
import nancal.mp.util.setNamespace
import nbcp.base.comm.JsonMap
import nbcp.base.extend.FromYamlText
import nbcp.base.extend.getValueByWbsPath
import nbcp.base.utils.SpringUtil
import org.junit.jupiter.api.Test
import javax.sql.DataSource

//
//@Configuration("aafe")
//@AutoConfigureAfter(DataSourceAutoConfiguration::class)
//@ConditionalOnBean(DataSourceAutoConfiguration::class)
//class MySqlConfig {
//    @Bean()
//    @Primary
//    @ConfigurationProperties("spring.datasource.hikari")
//    fun primaryDataSource(): DataSource? {
//        var ret = DataSourceBuilder.create().build() as HikariDataSource?
//        if (ret == null) {
//            return null;
//        }
//
//        if (ret.jdbcUrl == null) {
//            ret.jdbcUrl = SpringUtil.context.environment.getProperty("spring.datasource.url")
//        }
//
//        if (ret.driverClassName == null) {
//            ret.driverClassName = SpringUtil.context.environment.getProperty("spring.datasource.driverClassName").AsString("org.mariadb.jdbc.Driver")
//        }
//
//        if (ret.username == null) {
//            ret.username = SpringUtil.context.environment.getProperty("spring.datasource.username")
//        }
//
//        if (ret.password == null) {
//            ret.password = SpringUtil.context.environment.getProperty("spring.datasource.password")
//        }
//
//        return ret;
//    }
//
//    @Bean("slave")
//    @ConfigurationProperties("spring.datasource.slave.hikari")
//    @Conditional(ExistsSlaveDataSourceConfigCondition::class)
////    @ConditionalOnExpression("\${spring.datasource.slave}")
//    fun slaveDataSource(): DataSource? {
//        var ret = DataSourceBuilder.create().build() as HikariDataSource?
//
//        if (ret == null) {
//            return null;
//        }
//
//        if (ret.jdbcUrl == null) {
//            ret.jdbcUrl = SpringUtil.context.environment.getProperty("spring.datasource.slave.url")
//        }
//
//        if (ret.jdbcUrl == null) {
//            return null;
//        }
//
//        if (ret.driverClassName == null) {
//            ret.driverClassName = SpringUtil.context.environment.getProperty("spring.datasource.slave.driverClassName").AsString("org.mariadb.jdbc.Driver")
//        }
//
//
//
//        if (ret.username == null) {
//            ret.username = SpringUtil.context.environment.getProperty("spring.datasource.slave.username")
//        }
//
//        if (ret.password == null) {
//            ret.password = SpringUtil.context.environment.getProperty("spring.datasource.slave.password")
//        }
//
//        return ret;
//    }
//}

//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class FixData2 : TestBase() {
    @Test
    fun test1fff() {
        println("OK")
        var t = """
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  annotations: {}
  labels:
    k8s.kuboard.cn/layer: "mp"
    language: "Java"
  name: "mp-integration-java"
spec:
  progressDeadlineSeconds: 600
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      name: "mp-integration-java"
  template:
    metadata:
      labels:
        name: "mp-integration-java"
    spec:
      containers:
      - env:
        - name: "PROFILE"
          value: "dev,nacos"
        - name: "APP_GROUP"
          value: "default"
        image: "images.nancal.com:8000/java/mp-integration-java:20220528.200000"
        imagePullPolicy: "IfNotPresent"
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: "/health"
            port: 80
            scheme: "HTTP"
          initialDelaySeconds: 60
          periodSeconds: 60
          successThreshold: 1
          timeoutSeconds: 15
        name: "mp-integration-java"
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: "/health"
            port: 80
            scheme: "HTTP"
          initialDelaySeconds: 30
          periodSeconds: 6
          successThreshold: 1
          timeoutSeconds: 15
        terminationMessagePath: "/app/logs/termination-log"
        terminationMessagePolicy: "File"
        volumeMounts:
        - mountPath: "/app/skywalking"
          name: "skywalking-agent"
      dnsPolicy: "ClusterFirst"
      imagePullSecrets:
      - name: "myregistrykey"
      initContainers:
      - command:
        - "sh"
        - "-c"
        - "echo 'options use-vc' >> /etc/resolv.conf && echo 10000 > /proc/sys/net/core/somaxconn\
          \ && echo 'options single-request-reopen' >> /etc/resolv.conf"
        image: "busybox"
        imagePullPolicy: "IfNotPresent"
        name: "init"
        securityContext:
          privileged: true
        terminationMessagePath: "/dev/termination-log"
        terminationMessagePolicy: "File"
      - args:
        - "-c"
        - "cp -R /skywalking/agent /agent/"
        command:
        - "/bin/sh"
        image: "images.nancal.com:8000/tools/skywalking-java-agent:8.9.0-java11"
        imagePullPolicy: "IfNotPresent"
        name: "agent-container"
        terminationMessagePath: "/dev/termination-log"
        terminationMessagePolicy: "File"
        volumeMounts:
        - mountPath: "/agent"
          name: "skywalking-agent"
      restartPolicy: "Always"
      schedulerName: "default-scheduler"
      terminationGracePeriodSeconds: 30
      volumes:
      - emptyDir: {}
        name: "skywalking-agent"


       """

        var map = t.FromYamlText<JsonMap>()
        map.setNamespace("OK")
        println(map.getValueByWbsPath("spec.template.spec.containers[].env"))
        println("OK")
    }
    @Test
    fun ddd() {
        val client = ClientBuilder().setBasePath("http://192.168.5.213:6443").setVerifyingSsl(false)
            .setAuthentication(AccessTokenAuthentication("eyJhbGciOiJSUzI1NiIsImtpZCI6IkZjalVmUFlyeF9ZSmNFaHJ6NUdJclNBdERCdWloNkE4SUV0MWRXS19YTGcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4ta2xwbTgiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiODk1Njk5MGQtYjgwNC00ODFmLTg4YWMtNWM0MDMyMzBhNjJlIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.UMaOkWBk7aUp1ahZl316xIY_GwRw22uEFUIo9yMpE92vFlL1voHlq7vxb6s6zhPjeFON8y8658U3qam4UQqkDfIT5UYWzryWwB6V0IrZzunzoX0WZWB2-BMX3qSnAxmPSaaqbi_WTb1S4R54oNzZaOWkYsinYr4h1tyXYZR737yNY4ZV9mgs72zY52wkOJA8zskmWMAwm9Z8aZiDBN3p-Sx_PKAUBmjYJn5yZlOfWtWSabVqbpS2lrrVpnYXeAWvRs34e7tI1eM3Yq5sRPtiUNvvvGTT_CVC4P7Pui9sMcK1TMWFLb2wM6T05UHXlkB2CjjVzTDRi_PUyc3G9yon3g")).build()

        Configuration.setDefaultApiClient(client)

        var api = CustomObjectsApi();
        api.apiClient = client;

    }

    val abc: String by lazy {
        ddd();
        return@lazy "abc"
    }

    @Test
    fun test3() {
        var d = SpringUtil.getBean<DataSource>() as HikariDataSource
        println(d.jdbcUrl)
        println(d.maximumPoolSize)

        println(d.password)
    }
}